package JAVA300.第十二章网络编程;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 *练习网络爬虫--解决某些有权限的网站
 * 模拟浏览器
 */

public class Web03_SpiderTest02 {
    public static void main(String[] args) {

        try {
            //获取URL
            URL url = new URL("https://www.dianping.com");
            //下载资源--IO操作

            //InputStream inputStream = url.openStream();//字节流
            //1.借助HttpURLConnection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //2.借助http的请求协议
            conn.setRequestMethod("GET");//设置请求方式 有get 和post
            //拷贝网页中的user-Agent模拟get请求
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36");
            //3.获取输入流, 后面操作就是IO操作了
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));


            String msg = null;
            while ( null != (msg= reader.readLine())){
                System.out.println(msg);
            }
            reader.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
